<?php
/* Date create: 7/5/2011
 * Created by: Ngô Chí Công
 * Description: class mainPageXML 
 */
 class mainpageXML{
 	public  $domXml; 
 	public  $path_xml;

 	private $dom_links;   
 	private $dom_vips; 	
 	private $dom_advs;   
 	private $dom_slogans; 
 	private $dom_professions; 

 	public function init(){
 		$this->dom_links 			=	$this->domXml->getElementsByTagName("links")->item(0);
 		$this->dom_vips 			=	$this->domXml->getElementsByTagName("vips")->item(0);
 		$this->dom_advs 			=	$this->domXml->getElementsByTagName("advs")->item(0);
 		$this->dom_slogans 			=	$this->domXml->getElementsByTagName("slogans")->item(0);
 		$this->dom_professions 		=	$this->domXml->getElementsByTagName("professions")->item(0);
 	}
 	public function parseXML(){
		$xml 		  =	$this->domXml->saveXML();
		return $xml;
	}
	public function saveXML(){
		file_put_contents($this->path_xml, $this->parseXML());
	}
	function removeAllChildNodes($node){
		while ($node->hasChildNodes())
			$node->removeChild($node->firstChild);       
	}
 	public function __construct($path_file_mainpage_xml){
		$this->path_xml   = $path_file_mainpage_xml;
		if(file_exists($path_file_mainpage_xml)){
			$this->domXml = new DOMDocument('1.0', 'UTF-8');
			$this->domXml->load($path_file_mainpage_xml);		
		}else{			
			$defaultXml = '<mainpage>
								<links></links>
								<vips></vips>
								<advs></advs>
								<slogans></slogans>
								<professions></professions>
							</mainpage>';		
			$this->domXml = new DOMDocument('1.0', 'UTF-8');
	 		$this->domXml->loadXML($defaultXml);						
			file_put_contents($this->path_xml, $this->parseXML());
		}
		$this->init();
 	}
  	public function getLinks(){
 		$links = $this->dom_links->getElementsByTagName("link");
 		$arr   = Array();
 		if($links)
	 		foreach($links as $index=>$item){
	 			$link = Array();
		 			foreach($item->attributes as $attName=>$attrNode)
		 				$link[$attName] = $attrNode->nodeValue;
		 		$arr[] = $link;
	 		}
 		return $arr;
 	}
	/* array(
				0=>array('name'=>'...','url'=>'...'),
				1=>...
			)
	*/
	public function setLinks($arr_links){
		$this->removeAllChildNodes($this->dom_links);	
		if($arr_links){
			foreach($arr_links as $item){
				$nodeLink = $this->domXml->createElement("link");
				$nodeLink->setAttribute("name",$item["name"]);
				$nodeLink->setAttribute("name_en",$item["name_en"]);
				$nodeLink->setAttribute("url",$item["url"]);				
				$this->dom_links->appendChild($nodeLink);
			}			
		}
		$this->saveXML();
	}
	public function getVips(){
 		$vips = $this->dom_vips->getElementsByTagName("vip");
 		$arr   = Array();
 		if($vips)
	 		foreach($vips as $item){
	 			$vip = Array();
		 			foreach($item->attributes as $attName=>$attrNode)
		 				$vip[$attName] = $attrNode->nodeValue;
		 		$arr[] = $vip;
	 		}
 		return $arr;	
	}
	/* array(
				0=>array('img_url'=>'...','vip_name'=>'...','qoute'=>'...','vip_positions'=>'...'),
				1=>...
			)
	*/
	public function setVips($arr_vips){
		$this->removeAllChildNodes($this->dom_vips);	
		if($arr_vips){
			foreach($arr_vips as $item){
				$nodeVip = $this->domXml->createElement("vip");
				$nodeVip->setAttribute("img_url",$item["img_url"]);
				$nodeVip->setAttribute("vip_name",$item["vip_name"]);
				
				$nodeVip->setAttribute("qoute",$item["qoute"]);
				$nodeVip->setAttribute("qoute_en",$item["qoute_en"]);
				
				$nodeVip->setAttribute("vip_positions",$item["vip_positions"]);
				$nodeVip->setAttribute("vip_positions_en",$item["vip_positions_en"]);
				$this->dom_vips->appendChild($nodeVip);
			}			
		}
		$this->saveXML();
	}
	public function getAdvs(){
 		$advs = $this->dom_advs->getElementsByTagName("adv");
 		$arr   = Array();
 		if($advs)
	 		foreach($advs as $item){
	 			$adv = Array();
		 			foreach($item->attributes as $attName=>$attrNode)
		 				$adv[$attName] = $attrNode->nodeValue;
		 		$arr[] = $adv;
	 		}
 		return $arr;			
	}
	/* array(
				0=>array('img_url'=>'...','link'=>'...'),
				1=>...
			)
	*/
	public function setAdvs($arr_advs){
		$this->removeAllChildNodes($this->dom_advs);	
		if($arr_advs){
			foreach($arr_advs as $item){
				$nodeAdv = $this->domXml->createElement("adv");
				$nodeAdv->setAttribute("img_url",$item["img_url"]);
				$nodeAdv->setAttribute("link",$item["link"]);
				$this->dom_advs->appendChild($nodeAdv);
			}			
		}
		$this->saveXML();
	}	
	public function getSlogans(){
 		$slogans = $this->dom_slogans->getElementsByTagName("slogan");
 		$arr   = Array();
 		if($slogans)
	 		foreach($slogans as $item){
	 			$slogan = Array();
		 			foreach($item->attributes as $attName=>$attrNode)
		 				$slogan[$attName] = $attrNode->nodeValue;
		 		$arr[] = $slogan;
	 		}
 		return $arr;			
	}
	/* array(
				0=>array('qoute'=>'...'),
				1=>...
			)
	*/
	public function setSlogans($arr_slogans){
		$this->removeAllChildNodes($this->dom_slogans);	
		if($arr_slogans){
			foreach($arr_slogans as $item){
				$nodeSlogan = $this->domXml->createElement("slogan");
				$nodeSlogan->setAttribute("qoute",$item["qoute"]);
				$nodeSlogan->setAttribute("qoute_en",$item["qoute_en"]);
				$this->dom_slogans->appendChild($nodeSlogan);
			}			
		}
		$this->saveXML();
	}	
	public function getProfessions(){
 		$professions = $this->dom_professions->getElementsByTagName("profession");
 		$arr   = Array();
 		if($professions)
	 		foreach($professions as $item){
				$profession = Array();
				$profession["name"] 	  = $item->getAttribute("name");
				$profession["name_en"] 	  = $item->getAttribute("name_en");
				$profession["img_url"]    = $item->getAttribute("img_url");
				$profession["enterprise"] = array();
				
				$enterprises = $item->getElementsByTagName("enterprise");
				if($enterprises){
					foreach($enterprises as $item_enterprise){
						$profession["enterprise"][] = $item_enterprise->getAttribute("id");
					}
				}	
		 		$arr[] = $profession;
	 		}
 		return $arr;				
	}
	/* array(
				0=>array('name'=>'...','img_url'=>'...','enterprises'=>array(1,2,3,4...)),
				1=>...
			)
	*/
	public function setProfessions($arr_professions){
		$this->removeAllChildNodes($this->dom_professions);	
		if($arr_professions){
			foreach($arr_professions as $item){
				$nodeProfession = $this->domXml->createElement("profession");
				$nodeProfession->setAttribute("name",$item["name"]);
				$nodeProfession->setAttribute("name_en",$item["name_en"]);
				$nodeProfession->setAttribute("img_url",$item["img_url"]);
				if(count($item['enterprises'])){			
					foreach($item['enterprises'] as $itemEnterprise){
						$enterprise = $this->domXml->createElement("enterprise");
						$enterprise->setAttribute("id",$itemEnterprise);
						$nodeProfession->appendChild($enterprise);
					}
				}
				$this->dom_professions->appendChild($nodeProfession);
			}			
		}
		$this->saveXML();
	}		
 }
?>